package com.example.controller;

import com.example.entity.Check;
import com.example.entity.StudentAnswer;
import com.example.service.*;
import com.example.utils.Msg;
import com.example.utils.Time;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/back")
public class BackController {

    @Resource
    TeacherService teacherService;
    @Resource
    StudentService studentService;
    @Resource
    SubjectService subjectService;
    @Resource
    UserService userService;
    @Resource
    StudentAnswerService studentAnswerService;

    /**
     * 后台首页
     *
     * @return
     */
    @GetMapping("/")
    public String Init() {
        return "redirect:/back/index";
    }

    /**
     * 后台首页
     *
     * @return
     */
    @GetMapping("/index")
    public String Index(Model model, Authentication authentication) {
        if(userService.IsFirstLogin(authentication.getName(),false)&& authentication.getName()!=null){
            return "redirect:/front/password/1/0";
        }
        userService.UpdateLoginTime(authentication.getName(), Time.getTime(),false);
        model.addAttribute("bookList", teacherService.getMyBooks(authentication.getName()));
        return "back/index";
    }



    @GetMapping("/getCheck")
    public String getCheck(Model model,
                           @RequestParam(value = "book_name") String book_name) {
        List<StudentAnswer> answerList = studentAnswerService.getAnswerCheck(book_name);
        List<Check> ischeckList = new ArrayList<>();
        List<Check> notcheckList = new ArrayList<>();
        for (StudentAnswer studentAnswer : answerList) {
            studentAnswer.setAnswer(subjectService.getAnswer(studentAnswer));
            Check check = new Check();
            check.setStudentAnswer(studentAnswer);
            check.setStudent(studentService.queryOne(check.getStudentAnswer().getStudentNumber()));
            check.setStem(subjectService.getStem(check.getStudentAnswer()));
            if (studentAnswer.getScore() == null) {
                notcheckList.add(check);
            } else ischeckList.add(check);
        }
        model.addAttribute("isCheckList", ischeckList);
        model.addAttribute("notCheckList", notcheckList);
        return "back/Check/CheckCap";
    }

    @ResponseBody
    @PostMapping("/check/updateScore")
    public Msg updateScore(@RequestParam(value = "stu_num") String stu_num,
                           @RequestParam(value = "menu_topic") String menu_topic,
                           @RequestParam(value = "score") int score,
                           @RequestParam(value = "questionNo") String questionNo) {
        int result = subjectService.updateScore(menu_topic, questionNo, stu_num, score);
        if (result != 0) return Msg.success();
        else return Msg.fail();
    }
}
